<?php


namespace App\Http\Controllers\User;


use App\Http\Controllers\Controller;
use App\Models\User\Address;
use App\Models\User\Collection;
use Illuminate\Http\Request;
use Illuminate\Database\QueryException;
use Validator;
/**
 * @group 收藏信息
 *
 * 采用 restful api
 *
 * Body Parameters 表示请求的所需要的参数
 *
 * Query Parameters 表示返回参数的名称
 */
class CollectionController extends Controller
{
    /**
     * 收藏信息
     *
     * @queryParam  user_id 用户id
     * @queryParam  business_id 商家id
     *
     */
    public function index(){

        $query = Collection::with('business')->get()->toArray();
        return response()->json($query,200);
    }
    /**
     * 收藏信息查看单条
     *
     * @queryParam  user_id 用户id
     * @queryParam  business_id 商家id
     *
     */
    public function show($pk){

        $data = Collection::with('business')->find($pk);
        if($data == []){
            return response()->json(["message"=>"没有该信息"],406);
        }else{
            return response()->json($data,200);
        }
    }
    /**
     * 收藏信息添加
     * @bodyParam  business_id  required 商家id
     * 202 删除成功
     * 201 添加成功
     */
    public function store(Request $request)
    {

        try{
            $e = genValidate();
            $validator = Validator::make($request->all(),$e);
            if($validator->fails()){
                return response()->json($validator->errors()->messages(),400);
            }
            $type=$request->user_id;
            $data=$request->all();
            $data['user_id'] =$type;
            $row=Collection::where($data)->first();
            if ($row){
                Collection::where('id',$row['id'])->delete();
                return response()->json(["message"=>"删除成功"],202);
            }else{
                Collection::create($data);
                return response()->json(["message"=>"添加成功"],201);
            }

        }catch (QueryException $e){
            return response()->json(["message"=>"添加失败"],504);
        }
    }
    /**
     * 收藏信息修改
     * @bodyParam  business_id  required 商家id
     *
     * 503 修改失败
     */
    public function update(Request $request, $pk){
        $e = genValidate();
        $validator = Validator::make($request->all(),$e);
        if($validator->fails()){
            return response()->json($validator->errors()->messages(),400);
        }
        try {
            $dd=Collection::find($pk);

            if ($request->has('business_id')) {
                $dd->phone = $request->get('business_id');
            }
            $dd->save();
            return response()->json(["message"=>"修改成功"],200);
        }catch (QueryException $e){
            return response()->json(["message"=>"修改失败"],503);
        }
    }
    /**
     * 收藏信息删除
     *
     */
    public function destroy(Request $request,$pk){
        $data = Collection::find($pk);
        if($data == []){
            return response()->json(["message"=>"没有该信息"],404);
        }
        $data->delete();
        return response()->json(["message"=>"删除成功"],200);
    }
}
